package cn.newgrand.netcall.plugins.sms;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.TaskEngine;
import org.jivesoftware.spark.util.log.Log;


public class SMSGetbackPasswordPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private JLabel usernameLabel = new JLabel();
	private JTextField usernameField = new JTextField();
	
	private JLabel serverLabel = new JLabel();
    private JTextField serverField = new JTextField();

    private JButton sendSMSButton = new JButton();
    private JButton closeButton = new JButton();
    
    private JDialog dialog;

    public SMSGetbackPasswordPanel(String username,String serverAddress){
    	ResourceUtils.resLabel(usernameLabel, usernameField, "用户名:");
        ResourceUtils.resLabel(serverLabel, serverField, "服务器:");
        ResourceUtils.resButton(sendSMSButton, "发送短信");
        ResourceUtils.resButton(closeButton, "关闭");
        
        setLayout(new GridBagLayout());
        
        if(username != null){
        	usernameField.setText(username);
        }
        
        if(serverAddress != null){
        	serverField.setText(serverAddress);
        }
        
     // Add component to UI
        add(usernameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        add(usernameField, new GridBagConstraints(1, 0, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 150, 0));

        add(serverLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        add(serverField, new GridBagConstraints(1, 1, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        add(sendSMSButton, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        //add(closeButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        
        sendSMSButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
            	if(!isVaild()){
            		return;
            	}
            	Runnable getPassword = new Runnable() {
					
					@Override
					public void run() {
						try {
		            		StringBuffer buffer = new StringBuffer();
		            		buffer.append("http://").append(getServer()).append(":9090/plugins/integration/service/SendMessage?wsdl");
		            		//Log.error("---"+buffer.toString());
		            		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		            		//Log.error("---2"+factory);
		        	    	org.apache.cxf.endpoint.Client client = factory.createClient(buffer.toString());
		        	    	//Log.error("---3"+client);
		        	    	String result = (String)client.invoke("getBackPassword", getUsername())[0];
		        	    	//Log.error("---4"+result);
		        	    	String message = "";
		        	    	if("success".equals(result)){
		        	    		message = "短信发送成功!";
		        	    		dialog.dispose();
		        	    	}else if("unbind".equals(result)){
		        	    		message = getUsername()+"账号未绑定手机号码.";
		        	    	}else if("fail".equals(result)){
		        	    		message = "短信发送失败";
		        	    		dialog.dispose();
		        	    	}else if("userNotExist".equals(result)){
		        	    		message = getUsername() + "账号不存在.";
		        	    	}
		        	    	JOptionPane.showMessageDialog(dialog, message, "提示", JOptionPane.INFORMATION_MESSAGE);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(dialog, "确认服务器地址是否正确.", "提示", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				};
				TaskEngine.getInstance().submit(getPassword);
            	
            	
            }
        });

        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                dialog.dispose();
            }
        });
    
    }
    
    private boolean isVaild() {
    	boolean b = false;
    	boolean errors = false;
        String errorMessage = "";

        if (!ModelUtil.hasLength(getUsername())) {
            errors = true;
            usernameField.requestFocus();
            errorMessage = "请输入您要找回的密码的账号!";
        }else if(!ModelUtil.hasLength(getServer())){
        	errors = true;
            serverField.requestFocus();
            errorMessage = "请输入服务器的地址!";
        }
        
        if(errors){
        	JOptionPane.showMessageDialog(this, errorMessage, "提示", JOptionPane.ERROR_MESSAGE);
        }else{
        	b = true;
        }
		return b;
	}
    
    public void invoke(JFrame parent) {
        dialog = new JDialog(parent, "找回密码", true);

        //TitlePanel titlePanel = new TitlePanel("短信找回密码", "系统会将密码发送到您绑定的手机上.", null, true);
        dialog.getContentPane().setLayout(new BorderLayout());
        //dialog.getContentPane().add(titlePanel, BorderLayout.NORTH);
        dialog.getContentPane().add(this, BorderLayout.CENTER);
        dialog.pack();
        dialog.setSize(260, 160);
        dialog.setLocationRelativeTo(parent);
        dialog.setVisible(true);
    }
    
    
    public String getServer() {
        return serverField.getText();
    }
    
    public String getUsername() {
        return StringUtils.escapeNode(usernameField.getText().toLowerCase());
    }

}
